﻿using Abp.Application.Services;
using Abp.Domain.Repositories;
using Abp.Web.Models;
using Batman.Jianbei.Forum.Core.Consts;
using Batman.Jianbei.Forum.Core.Domains;
using Batman.Jianbei.Forum.Core.Domains.Users;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace Batman.Jianbei.Forum.Services.Users
{
    public class UserRegister : ApplicationService
    {
        public UserManager<User> userManager { get; set; }

        public IRepository<WeixinUser, string> weixinRepo { get; set; }

        /// <summary>
        /// 微信OpenId注册
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        //public async Task<AjaxResponse> WeixinTryRegist(WeixinUser weixinUser)
        //{
            //var matched = weixinRepo.GetAll().AsNoTracking().FirstOrDefault(t => t.OpenId == weixinUser.OpenId);

            //User user = null;

            //if (matched == null)
            //{
                //var newUser = new User { UserName = Guid.NewGuid().ToString() };
                //var createResult = await userManager.CreateAsync(newUser);

                //if (!createResult.Succeeded)
                //{
                //    return new AjaxResponse { Success = false, Result = createResult.Errors.ToList().Select(t => t.Description) };
                //}

                //weixinUser.UserId = newUser.Id;
                //weixinUser = weixinRepo.Insert(weixinUser);

                //user = newUser;
            //}
            //else
            //{
            //    user = userManager.Users.AsNoTracking().Where(t => t.Id == matched.UserId).FirstOrDefault();
            //}


            //return new AjaxResponse { Success = true, Result = user };
        //}
    }
}
